<?php

class Search extends CI_Controller {

    public $search;
    public $mode;
    public $urutan;
    public $nitem;

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('pagination');
        $this->load->library('session');
        $this->config->load('artyproperty');
        $this->_public_view = $this->config->item('public_view');
    }

    public function index() {
        //cek session
        $isLogin = $this->session->userdata('isLogin');
        $user = '';
        if ($isLogin) {
            $user = $this->session->userdata('username');
        }

        $this->session->unset_userdata('search');
        $this->session->unset_userdata('mode');
        $data = array('page_content' => 'home',
            'page_title' => 'Pencarian',
            'isLogin' => $isLogin,
            'user' => $user);
        $this->load->view($this->_public_view, $data);
    }

    public function cari($page = 1) {
        //cek session
        $isLogin = $this->session->userdata('isLogin');
        $user = '';
        if ($isLogin) {
            $user = $this->session->userdata('username');
        }

        $this->session->set_userdata('page', 2);

        $sql = "SELECT nama FROM tipe_properti";
        $tipe = $this->db->query($sql);
        $sql = "SELECT nama_sertifikat FROM sertifikasi";
        $sertifikat = $this->db->query($sql);
        $sql = "SELECT kota_kab FROM alamat";
        $area = $this->db->query($sql);

        $this->search = $this->input->get('search', TRUE);
        $this->mode = $this->input->get('mode', TRUE);
        $this->urutan = $this->input->get('urutan', TRUE);
        $this->nitem = $this->input->get('nitem', TRUE);
        $this->tipe = $this->input->get('tipe', TRUE);
        $this->mandi = $this->input->get('mandi', TRUE);
        $this->tidur = $this->input->get('tidur', TRUE);
        $this->luasmin = $this->input->get('luasmin', TRUE);
        $this->luasmaks = $this->input->get('luasmaks', TRUE);
        $this->hargamin = $this->input->get('hargamin', TRUE);
        $this->hargamaks = $this->input->get('hargamaks', TRUE);
        $this->sertifikat = $this->input->get('sertifikat', TRUE);

        if ($this->mode == "") {
            $this->mode = "1";
        }
        if ($this->nitem == "") {
            $this->nitem = "10";
        }

        //cari
        $sql = "SELECT * FROM
            (SELECT properti.*, COUNT(ip_address) AS count
            FROM
            properti LEFT OUTER JOIN hit
            ON properti.properti_id = hit.properti_id
            GROUP BY properti.properti_id) AS properti 
            NATURAL JOIN alamat
            NATURAL JOIN sertifikasi
            NATURAL JOIN furnitur
            NATURAL JOIN tipe_properti,
            user
                WHERE
                properti.user_id = user.user_id
                AND sewa_beli = " . $this->mode . "
                AND properti.tipe like '%" . $this->tipe . "%'
                AND jml_kmr_tidur like '%" . $this->tidur . "%'
                AND jml_kmr_mandi like '%" . $this->mandi . "%'";
        if ($this->luasmin && $this->luasmaks) {
            $sql = $sql .
                    " AND luas_bangunan >= " . $this->luasmin . "
                AND luas_bangunan <= " . $this->luasmaks;
        }
        if ($this->hargamin && $this->hargamaks) {
            $sql = $sql .
                    " AND harga >= " . $this->hargamin . "
                AND harga <= " . $this->hargamaks;
        }
        $sql = $sql .
                " AND sertifikasi like '%" . $this->sertifikat . "%'
                    AND (nama_properti like '%" . $this->search . "%'
                    OR jalan like '%" . $this->search . "%'
                    OR kota_kab like '%" . $this->search . "%'
                    OR provinsi like '%" . $this->search . "%'    
                    OR negara like '%" . $this->search . "%')
                     ";
        if ($this->urutan == "6") {
            $sql = $sql .
                    "ORDER BY luas_bangunan ASC";
        } else if ($this->urutan == "5") {
            $sql = $sql .
                    "ORDER BY luas_bangunan DESC";
        } else if ($this->urutan == "4") {
            $sql = $sql .
                    "ORDER BY harga DESC";
        } else if ($this->urutan == "3") {
            $sql = $sql .
                    "ORDER BY harga ASC";
        } else if ($this->urutan == "2") {
            $sql = $sql .
                    "ORDER BY count DESC";
        } else {
            $sql = $sql .
                    "ORDER BY tgl_update DESC";
        }
        $query = $this->db->query($sql);
        $result = array_slice($query->result(), ($page - 1) * $this->nitem, $this->nitem);

        if (count($_GET) > 0)
            $config['suffix'] = '?' . http_build_query($_GET, '', "&");
        $config['base_url'] = site_url('search/cari/');
        $config['first_url'] = $config['base_url'] . '?' . http_build_query($_GET);
        $config['total_rows'] = $query->num_rows();
        $config['per_page'] = $this->nitem;
        $config['use_page_numbers'] = TRUE;
        $config['num_links'] = 10;
        //$config['num_tag_open'] = '<span class="page">';
        //$config['num_tag_close'] = '</span>';
        $config['first_link'] = 'Pertama';
        $config['last_link'] = 'Terakhir';
        $config['next_link'] = 'Berikutnya';
        $config['prev_link'] = 'Sebelumnya';
        $this->pagination->initialize($config);

        $data = array('page_content' => 'cari_properti',
            'page_title' => 'Hasil Pencarian',
            'search' => $this->search,
            'result' => $result,
            'urutan' => $this->urutan,
            'nitem' => $this->nitem,
            'page' => $page,
            'isLogin' => $isLogin,
            'user' => $user,
            'tipe' => $tipe,
            'sertifikat' => $sertifikat,
            'area' => $area);
        $this->load->view($this->_public_view, $data);
    }

    public function search_handler($search) {
        if ($search) {
            echo "ada query baru<br/>";
            $this->session->set_userdata('search', $search);
            return $search;
        } elseif ($this->session->userdata('search')) {
            echo "ambil dari session<br/>";
            $search = $this->session->userdata('search');
            return $search;
        } else {
            echo "search default value";
            return "";
        }
    }

    public function mode_handler($mode) {
        if ($mode) {
            $this->session->set_userdata('mode', $mode);
            return $mode;
        } elseif ($this->session->userdata('mode')) {
            $mode = $this->session->userdata('mode');
            return $mode;
        } else {
            $this->session->set_userdata('mode', "1");
            return "1";
        }
    }

    public function nitem_handler($nitem) {
        if ($nitem) {
            $this->session->set_userdata('nitem', $nitem);
            return $nitem;
        } elseif ($this->session->userdata('nitem')) {
            $nitem = $this->session->userdata('nitem');
            return $nitem;
        } else {
            return 10;
        }
    }

    public function urutan_handler($urutan) {
        if ($urutan) {
            $this->session->set_userdata('urutan', $urutan);
            return $urutan;
        } elseif ($this->session->userdata('urutan')) {
            $urutan = $this->session->userdata('urutan');
            return $urutan;
        } else {
            return "";
        }
    }

}

?>